fix: handle missing content-type header in Request.post_data_json#3095
Merged
Conversation
post_data_json indexed self.headers["content-type"], raising KeyError when a POST request had a body but no content-type header. Read the header defensively with .get() and guard the form-urlencoded branch so a missing header falls through to JSON parsing, matching the upstream JS implementation. Closes microsoft#3093 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
dgozman
approved these changes
Jun 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Request.post_data_jsonindexedself.headers["content-type"], which raisesKeyErrorwhen a POST request has a body but nocontent-typeheader:The documented behavior is to parse form-urlencoded bodies when that content-type is set, and otherwise fall back to JSON parsing. A missing header should take the JSON fallback path. This matches the upstream JS implementation, where
this.headers()['content-type']isundefined-safe before the form-urlencoded branch.Closes #3093
Fix
In
playwright/_impl/_network.py, read the header defensively and guard the branch:Tests
Added to
tests/async/test_network.py:test_should_parse_the_json_post_data_when_content_type_header_is_missing— the regression guard. Usesrequest.headers.delete('content-type')to produce a request whosecontent-typekey is genuinely absent (unlike setting it to'', which keeps an empty-valued key), then assertspost_data_jsonreturns the parsed JSON. Verified to fail withKeyErroron the unpatched code.test_should_return_post_data_for_put_requests— mirrors the upstreamnetwork-post-datasuite for parity.All existing network tests continue to pass.
Notes
api.jsonupdates are needed.